﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HSHCallDataDeal;
using NPOI.HSSF.UserModel;
using ZMDTech.SCEntWeb.Web.App_Start;

namespace TestConsoleApplication
{
    static class Program
    {
        static void Main(string[] args)
        {
            ReadExcel();
            ////CreateExcel();
            //UseAggregate();
            Console.Read();
        }

        #region NopIde使用

        /// <summary>
        /// 读取Excel数据
        /// </summary>
        public static void ReadExcel()
        {
            var path = @"F:\tel2.xls";//文件地址
            DataTable dt = NPOIHelper.Import(path, 1);
            var rows = dt.Rows;
            var clums = dt.Columns;
            foreach (DataRow item in rows)
            {
                var tel = item["电话"].ToString();
                var name = item["姓名"].ToString();
                //直接读取
                //foreach (DataColumn colum in clums)
                //{
                // Console.Write(item[colum].ToString() + "\t");
                //}
                using (var dbset = new Model1())
                {
                    try
                    {
                        dbset.Configuration.ValidateOnSaveEnabled = false; //不跟踪实体
                        var model = dbset.YWM_Member_UserInfo.FirstOrDefault(x => !x.IsDeleted && x.LoginAccount == tel);
                        if (model != null)
                        {
                            model.IsSign = true;
                            model.IsRealName = true;
                        }
                        dbset.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Console.Write("执行失败");
                        throw;
                    }
                   
                    Console.WriteLine();
                }
               
            }
             Console.Write("执行成功！");

        }
        /// <summary>
        /// 生成Excel
        /// </summary>
        public static void CreateExcel()
        {
            //创建工作薄
            var workbook = new HSSFWorkbook();
            //创建表
            var table = workbook.CreateSheet("joye.net");

            //模拟20行20列数据
            for (var i = 0; i < 20; i++)
            {
                var row = table.CreateRow(i);
                for (int j = 0; j < 20; j++)
                {
                    var cell = row.CreateCell(j);
                    cell.SetCellValue(j);
                }
            }
            //打开xls文件，如没有则创建，如存在则在创建是不要打开该文件
            using (var fs = File.OpenWrite(@"c:/joye.net.xls"))
            {
                workbook.Write(fs);   //向打开的这个xls文件中写入mySheet表并保存。
                Console.WriteLine("生成成功");
            }
        }
        #endregion


        #region 
        public static void UseAggregate()
        {
            //string[] words = new string[] { "Able", "was", "I", "ere", "I", "saw", "Elba" };
            //var result=new List<string>() { "Able", "was", "I", "ere", "I", "saw", "Elba" };
            //string s = result.Aggregate((a, n) => a + "-" + n);
            //Console.WriteLine(s);
            string[] words = new string[] { "Able", "was", "I", "ere", "I", "saw", "Elba" };
            string normal = words.Aggregate((a, n) => a + " " + n);
            string reverse = words.Aggregate((a, n) => n.Reverse() + " " + a);

            Console.WriteLine("正常：" + normal);
            Console.WriteLine("倒置：" + reverse);
        }
        // 倒置字符串，输入"abcd123"，返回"321dcba"
        public static string Reverse(this string value)
        {
            char[] input = value.ToCharArray();
            char[] output = new char[value.Length];
            for (int i = 0; i < input.Length; i++)
                output[input.Length - 1 - i] = input[i];
            return new string(output);
        }
        #endregion
    }
}
